package com.cloudbroker.bcs.common.util;

import java.security.SecureRandom;
import java.util.Random;

public class RandomUtil {
    
    private static final String NUMERIC = "0123456789";
    
    private static final String LOWERCASE_ALPHA = "abcdefghijklmnopqrstuvwxyz";
    
    private static final String UPPERCASE_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXWZ";
    
    private static final String ALPHA = LOWERCASE_ALPHA + UPPERCASE_ALPHA;
    
    private static final String NUMERIC_ALPHA = NUMERIC + ALPHA;
    
    public static String genRandomString(int length) {
        return genNumericAlphaRandomString(length);
    }
    
    public static String genNumericRandomString(int length) {
        return genRandomString(NUMERIC, length);
    }
    
    public static String genLowerAlphaRandomString(int length) {
        return genRandomString(LOWERCASE_ALPHA, length);
    }
    
    public static String genUpperAlphaRandomString(int length) {
        return genRandomString(UPPERCASE_ALPHA, length);
    }
    
    public static String genAlphaRandomString(int length) {
        return genRandomString(ALPHA, length);
    }
    
    public static String genNumericAlphaRandomString(int length) {
        return genRandomString(NUMERIC_ALPHA, length);
    }
    
    public static String genRandomString(String dict, int length) {
        return genRandomString(dict, length, null);
    }
    
    public static String genRandomString(String dict, int length, Random random) {
        int dictLength = dict.length();
        if (null == random) {
            random = new Random();
        }
        StringBuilder sBuilder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char c = dict.charAt(random.nextInt(dictLength));
            sBuilder.append(c);
        }
        return sBuilder.toString();
    }
    
    public static String genSecureRandomString(String dict, int length) {
        return genRandomString(dict, length, new SecureRandom());
    }
    
    public static String genNumericAlphaSecureRandomString(int length) {
        return genSecureRandomString(NUMERIC_ALPHA, length);
    }
}
